package com.hxzy.controller;

import com.hxzy.common.annon.Log;
import com.hxzy.common.annon.RequiresPermissions;
import com.hxzy.common.dto.ValueChangeDTO;
import com.hxzy.common.enums.AckCode;
import com.hxzy.common.enums.BusinessType;
import com.hxzy.common.vo.PageVO;
import com.hxzy.common.vo.R;
import com.hxzy.common.web.controller.BaseController;
import com.hxzy.service.SysRoleService;
import com.hxzy.system.dto.RoleSaveDTO;
import com.hxzy.system.dto.SysRoleSearchDTO;
import com.hxzy.system.entity.SysRole;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;
import java.util.Set;

@Api(tags = "后台角色API")
@RestController
@RequestMapping(value = "/role")
public class SysRoleController extends ExistsValueController {


    @Autowired
    private SysRoleService sysRoleService;

  //  @PreAuthorize(value = "@ss.hasAuthority('system:role:list')")
    @RequiresPermissions(value ="system:role:list")
    @ApiOperation("角色信息分页")
    @GetMapping(value = "/data")
    public R<PageVO> search(SysRoleSearchDTO sysRoleSearchDTO){
        super.startPageHelper();
        List<SysRole> arr=this.sysRoleService.search(sysRoleSearchDTO);
        return super.pageToPageVO(arr);
    }


    @ApiOperation(value = "根据主键查询")
    @GetMapping(value = "/{id}")
    @RequiresPermissions(value ="system:role:query")
   // @PreAuthorize(value = "@ss.hasAuthority('system:role:query')")
    public R<SysRole> findById(@PathVariable(value = "id") Long id){
        SysRole sysRole = this.sysRoleService.getById(id);
        if(sysRole==null){
            return R.build(AckCode.NOT_FOUND_DATA);
        }
        return R.okHasData(sysRole);
    }

    @ApiOperation("新增")
   // @Log(title = "角色管理",businessType = BusinessType.INSERT)
   // @PreAuthorize(value = "@ss.hasAuthority('system:role:add')")
    @PostMapping
    public R<Boolean> add(@RequestBody @Valid RoleSaveDTO roleSaveDTO ) throws IllegalAccessException {
        SysRole role=new SysRole();
        BeanUtils.copyProperties(roleSaveDTO,role);
        //验证数据是否重复
        super.adminValidateExistsValue(role,"role_name", role.getRoleName());
        super.adminValidateExistsValue(role,"role_key", role.getRoleKey());

        boolean  result = this.sysRoleService.addAndGrantMenu(role, roleSaveDTO.getMenuIds());
        return super.toR(result);
    }

    @ApiOperation("修改")
   // @Log(title = "角色管理",businessType = BusinessType.UPDATE)
  //  @PreAuthorize(value = "@ss.hasAuthority('system:role:edit')")
    @PutMapping
    public R<Boolean> edit(@RequestBody @Valid RoleSaveDTO roleSaveDTO ) throws IllegalAccessException {
        SysRole role=new SysRole();
        BeanUtils.copyProperties(roleSaveDTO,role);
        //验证数据是否重复
        super.adminValidateExistsValue(role,"role_name", role.getRoleName());
        super.adminValidateExistsValue(role,"role_key", role.getRoleKey());
        boolean result=this.sysRoleService.editAndGrantMenu(role, roleSaveDTO.getMenuIds());
        return super.toR(result);
    }

    @ApiOperation("导出数据")
   // @Log(title = "角色信息", businessType = BusinessType.EXPORT)
  //  @PreAuthorize("@ss.hasAuthority('system:role:export')")
    @PostMapping(value = "/export")
    public void export(HttpServletResponse response, SysRoleSearchDTO sysRoleSearchDTO) throws IOException {
        List<SysRole> list = this.sysRoleService.search(sysRoleSearchDTO);
        super.writeExcel(response,"角色数据",SysRole.class,list);
    }

    @PreAuthorize(value = "@ss.hasAuthority('system:role:edit')")
   // @ApiOperation("状态修改")
   // @Log(title = "角色信息", businessType = BusinessType.UPDATE)
    @PutMapping("/changeStatus")
    public R changeStatus(@RequestBody @Validated ValueChangeDTO valueChangeDTO){
       // 如果是超级管理员id = 1的时候，不允许操作
        if (valueChangeDTO.getId().intValue() == 1){
            return  R.build(AckCode.NOT_ALLOW_CHANGED);
        }
        SysRole sysRole = new SysRole();
        sysRole.setId(valueChangeDTO.getId());
        sysRole.setStatus(valueChangeDTO.getValue());
        boolean result = this.sysRoleService.updateById(sysRole);
        return result ? R.ok() : R.build(AckCode.FAIL);
    }



}
